﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class ChangePartsAcceptDate : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] {Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentQltyList"; }
        }

        protected int QltyId
        {
            get
            {
                return GetRequestValue<int>("QltyId", 0);
            }
        }
        private AgentQltyInfo _objQlty = null;
        protected AgentQltyInfo objQlty
        {
            get
            {
                if (_objQlty == null)
                {
                    _objQlty = AgentQltyController.GetByQltyId(QltyId);
                }
                return _objQlty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHidePostBackReference();
            //权限判断
            //配件人员只能修改 配件需求为 索赔发货
            //IHISCE 服务内群 修改 为 供应商发货
            if (objQlty.ClaimState != ClaimState.品情已确认.ToString())
            {
                Alert.Show("此品情未确认,不能修改配件发货信息", "提示", MessageBoxIcon.Information,ActiveWindow.GetHidePostBackReference());
                return;
            }           
            
           

            this.ClaimsNumber.Text = objQlty.ClaimsNumber;
            this.IssueDate.Text = objQlty.IssueDate.ToString("yyyy-MM-dd");
            this.SerialNumber.Text = objQlty.SerialNumber;
            this.MachineType.Text = objQlty.MachineType;

            this.FaultDate.Text = objQlty.FaultDate.ToString("yyyy-MM-dd");
            this.WorkHour.Text = objQlty.WorkHour.ToString("F0");

            this.AgentAcceptDate.Text = objQlty.AgentAcceptDate == DateTime.MinValue ? "" : objQlty.AgentAcceptDate.ToString("yyyy-MM-dd");
            this.AgentAcceptDateTimePicker.Text= objQlty.AgentAcceptDate == DateTime.MinValue ? "" : objQlty.AgentAcceptDate.ToString("HH:mm");
            this.AgentRecoveryDate.Text = objQlty.AgentRecoveryDate == DateTime.MinValue ? "" : objQlty.AgentRecoveryDate.ToString("yyyy-MM-dd");
            this.AgentRecoveryDateTimePicker.Text = objQlty.AgentRecoveryDate == DateTime.MinValue ? "" : objQlty.AgentRecoveryDate.ToString("HH:mm");
            this.PartsInDate.Text = objQlty.PartsInDate == DateTime.MinValue ? "" : objQlty.PartsInDate.ToString("yyyy-MM-dd");
            this.PartsInDateTimePicker.Text = objQlty.PartsInDate == DateTime.MinValue ? "" : objQlty.PartsInDate.ToString("HH:mm");
            //加载配件
            DataTable listParts = AgentQltyController.GetAllPartsAndLogisticsByQltyId(QltyId);
            this.grdAgentQltyParts.DataSource = listParts;
            this.grdAgentQltyParts.DataBind();

            if (objQlty.PartsRequestType == "索赔结算" || objQlty.PartsRequestType == "无配件需求" || objQlty.PartsRequestType == "备用件发货")
            {
                this.AgentAcceptDate.Text = "";
                this.AgentAcceptDate.Hidden = true;
            }
            this.ImgHY.Text = objQlty.ImgHY;
            ///加载图片
            List<AgentQltyImgInfo> listImg = AgentQltyController.GetAllImgByQltyId_1(QltyId);
            try
            {
                for (int imgIndex = 1; imgIndex <=6 ; imgIndex++)
                {
                    //FineUI.FileUpload FileUpladControl = Panel4.FindControl("filePhoto" + imgIndex) as FineUI.FileUpload;                
                    FineUI.Image imgRelativelyControl = Panel4.FindControl("imgRelatively" + imgIndex) as FineUI.Image;
                    FineUI.TextBox ImgTitleControl = Panel4.FindControl("ImgTitle" + imgIndex) as FineUI.TextBox;

                    ImgTitleControl.Text = listImg[imgIndex - 1].Title;
                    imgRelativelyControl.ImageUrl = listImg[imgIndex - 1].IMGRelatively;
                }
            }
            catch { }

            try
            {
                // this.fileOther.Label = listImg[6].IMGRelatively;
                this.fileOtherDownLoad.NavigateUrl = listImg[6].IMGRelatively;
                // this.fileOtherDownLoad.Hidden = string.IsNullOrEmpty(listImg[6].IMGRelatively);
            }
            catch { }
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            DateTime _AgentAcceptDate = this.AgentAcceptDate.Text.IsEmptyOrNull() ? DateTime.MinValue : Convert.ToDateTime(this.AgentAcceptDate.Text+" "+this.AgentAcceptDateTimePicker.Text);
            DateTime _AgentRecoveryDate = this.AgentRecoveryDate.Text.IsEmptyOrNull() ? DateTime.MinValue : Convert.ToDateTime(this.AgentRecoveryDate.Text + " " + this.AgentRecoveryDateTimePicker.Text);
            DateTime _PartsInDate = this.PartsInDate.Text.IsEmptyOrNull() ? DateTime.MinValue : Convert.ToDateTime(this.PartsInDate.Text + " " + this.PartsInDateTimePicker.Text);

            List<AgentQltyImgInfo> imgList = new List<AgentQltyImgInfo>();
            AgentQltyImgInfo imgObj = null;
            for (int imgIndex = 1; imgIndex <= 6; imgIndex++)
            {
                imgObj = new AgentQltyImgInfo();
                FineUI.Image imgRelativelyControl = Panel4.FindControl("imgRelatively" + imgIndex) as FineUI.Image;
                FineUI.TextBox ImgTitleControl = Panel4.FindControl("ImgTitle" + imgIndex) as FineUI.TextBox;
                imgObj.Title = ImgTitleControl.Text;
                imgObj.IMGRelatively = imgRelativelyControl.ImageUrl;
                imgObj.FileIndex = imgIndex;
                imgObj.FromType = 1;
                imgList.Add(imgObj);
            }
            imgObj = new AgentQltyImgInfo();
            imgObj.Title = "其它文件";
            imgObj.IMGRelatively = fileOtherDownLoad.NavigateUrl;
            imgObj.FileIndex = 7;
            imgObj.FromType = 1;
            imgList.Add(imgObj);

            bool reVal = AgentQltyController.UpdatePartsAcceptDate(QltyId, _AgentAcceptDate, _AgentRecoveryDate, _PartsInDate, this.ImgHY.Text, imgList);
            if (reVal)
            {
                Alert.Show("已成功提交", "提示", MessageBoxIcon.Information,ActiveWindow.GetHidePostBackReference());
               
            }
            else
            {
                Alert.Show("提交失败,请联系IT部", "提示", MessageBoxIcon.Error);
            }
            // , DateTime ERPSendDate, DateTime RealSendDate
        }

        protected void grdAgentQltyParts_RowDataBound(object sender, GridRowEventArgs e)
        {
            DataRowView dr = e.DataItem as DataRowView;
            try
            {


                System.Web.UI.WebControls.Label lbSvrResult = grdAgentQltyParts.Rows[e.RowIndex].FindControl("lbU_InvoiceNum") as System.Web.UI.WebControls.Label;

                lbSvrResult.Text = SKuaiDi100Controller.GetKuaiDi100(dr["U_ExpressMode"].ToString(), dr["U_InvoiceNum"].ToString());// string.Format("<a href='#'  onclick=\"onviewkuaidi('{0}','{1}')\">{1}</a>", dr["U_ExpressMode"].ToString(), dr["U_InvoiceNum"].ToString());

                //execlClaims.NavigateUrl = "javascript:openExecl(" + dr["QltyId"] + ")";
            }
            catch
            {

            }
        }

        //protected void FileUploadHY_FileSelected(object sender, EventArgs e)
        //{
        //    //FineUI.FileUpload FileUpladControl = sender as FineUI.FileUpload;
        //    //string cIndex = FileUpladControl.EmptyText;
        //   // FineUI.Image imgRelativelyControl = Panel4.FindControl("imgRelatively" + cIndex) as FineUI.Image;



        //    if (FileUploadHY.HasFile)
        //    {
        //        string fileName = FileUploadHY.ShortFileName;

        //        if (!ValidateFileType(fileName))
        //        {
        //            Alert.Show("无效的文件类型！");
        //            return;
        //        }


        //        fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
        //        fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

        //        string path = GetUpLoadFilePath(User.Department);
        //        FileUploadHY.SaveAs(Server.MapPath(path + fileName));

        //        ImgHY.ImageUrl = path + fileName;

        //        // 清空文件上传组件
        //        FileUploadHY.Reset();
        //    }
        //}

        protected void DelteImage_Click(object sender, EventArgs e)
        {
            if (!HasPower(Permission.修改))
                return;
            FineUI.LinkButton btnControl = sender as FineUI.LinkButton;
            string cIndex = btnControl.TabIndex.ToString();
           
            FineUI.Image imgRelativelyControl = Panel4.FindControl("imgRelatively" + cIndex) as FineUI.Image;
            imgRelativelyControl.ImageUrl = "/res/blank.png";
            
        }

        protected void filePhoto1_FileSelected(object sender, EventArgs e)
        {
            FineUI.FileUpload FileUpladControl = sender as FineUI.FileUpload;
            string cIndex = FileUpladControl.EmptyText;
            FineUI.Image imgRelativelyControl = Panel4.FindControl("imgRelatively" + cIndex) as FineUI.Image;



            if (FileUpladControl.HasFile)
            {
                string fileName = FileUpladControl.ShortFileName;

                if (!ValidateFileType(fileName))
                {
                    Alert.Show("无效的文件类型！");
                    return;
                }


                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                string path = GetUpLoadFilePath(User.Department);
                FileUpladControl.SaveAs(Server.MapPath(path + fileName));

                imgRelativelyControl.ImageUrl = path + fileName;

                // 清空文件上传组件
                FileUpladControl.Reset();
            }
        }
        protected void fileOther_FileSelected(object sender, EventArgs e)
        {
            if (fileOther.HasFile)
            {
                string fileName = fileOther.ShortFileName;

                if (!ValidateFileType(fileName))
                {
                    Alert.Show("无效的文件类型！");
                    return;
                }


                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                string path = GetUpLoadFilePath(User.Department);
                fileOther.SaveAs(Server.MapPath(path + fileName));

                // fileOther.EmptyText = path + fileName;
                // fileOther.ShowEmptyLabel = false;
                fileOtherDownLoad.Hidden = false;
                fileOtherDownLoad.NavigateUrl = path + fileName;
                fileOtherDownLoad.Text = fileName;
                //  fileOther.Label = path + fileName;

                // 清空文件上传组件
                fileOther.Reset();
            }
        }
    }
}